package cn.tedu.shop.contorller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.RequestWrapper;

@Controller
public class shop_project {
    int count_shpping = 0;
    int total =0;
    String username1=null;
    String password1=null;
    //此方法用于接入网站,并进行登录验证
    @RequestMapping("/login")
    public String login(String username, String password) {
        //暂定初始用户为123456，初始密码为123456
        String user = "123456";
        String pad = "123456";
        password1=password;
        username1=username;
        System.out.println(username);
        System.out.println(password);
        if(user.equals(username) == true && pad.equals(password) == true){
            System.out.println("恭喜登录成功");
            return "index";
        }else {
            System.out.println("重新登录");
            return "login";
        }
    }
//    判定账号或密码是否为空
    public int nologin(String username,String password){

        if(username==null || password==null){
            return 1;
        }
        return 0;
    }
    //首页商品添加
    @RequestMapping("/calculate")
    public String calculate(String price,String shppingcart,String tag,Model m){
        int result=nologin(username1,password1);
        if(result==1){
            return "login";
        }
        //处理传回的字符串
        String newinx="";
        for(int i=0;i<price.length();i++){
//    .charAt()方法返回指定索引处的char值。索引范围是从0到length() - 1。对于数组索引，序列的第一个char值是在索引为0，索引1，依此类推
//       将 String中的非数字字符去掉
            if((int )'0'<=(int)price.charAt(i)&&(int)'9'>=(int)price.charAt(i))
                newinx+=price.charAt(i);
        }
//        将String类型的数据转为int类型
        int price1 = Integer.parseInt(newinx);

        total = total+price1;   // 商品总价的累加计算
        count_shpping += 1;     //计数商品的件数
        if(!tag.equals("no-skip")){
            m.addAttribute("count_shpping",count_shpping-1);   //将商品数量返回至前端
            m.addAttribute("total",total);
            return "shppingcar";
        }else{
            return "index";
        }
    }

    @RequestMapping("/settlement")
    public String s_ment(String s_ment,Model m){
        System.out.println(count_shpping);
        if(s_ment.equals("结算")){
            m.addAttribute("count_shpping",count_shpping-1);
            m.addAttribute("total",total);
            return "settlement";
        }
        return "shppingcar";
    }

    @RequestMapping("/development")
    public String development(String development){
        if(development.equals("+")){
            return "development";
        }
        return "shppingcar";
    }

    @RequestMapping(value = "/shppingcar",method = RequestMethod.GET)
    public String toPage1(HttpServletRequest request){
        String url=request.getParameter("shppingcar.html");
        return "shppingcar";
    }

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String toPage2(HttpServletRequest request){
        String url=request.getParameter("index.html");
        return "index";
    }


    @RequestMapping(value = "/settlement",method = RequestMethod.GET)
    public String toPage3(HttpServletRequest request,Model model){
        String url=request.getParameter("settlement.html");
        String total1 = total+"元";
        model.addAttribute("total1",total1);
        count_shpping--;
        String count_shpping1 = count_shpping+"件";
        model.addAttribute("count_shpping1",count_shpping1);
        return "settlement";
    }

    @RequestMapping(value = "/settlement_kaifa",method = RequestMethod.GET)
    public String toPage4(HttpServletRequest request){
        String url=request.getParameter("settlement_kaifa.html");
        return "settlement_kaifa";
    }

    @RequestMapping(value = "/development",method = RequestMethod.GET)
    public String toPage5(HttpServletRequest request){
        String url=request.getParameter("development.html");
        return "development";
    }
}


